<template>
  <div v-if="mounted" class="add-book-container">
    <!-- 头部 -->
    <div class="container">
      <!-- 表单卡片 -->
      <div class="form-card">
        <div class="form-header">
          <h2>📖 新增图书</h2>
        </div>

        <el-form
            ref="bookFormRef"
            :model="bookForm"
            :rules="formRules"
            label-width="120px"
            class="form-content"
            @submit.prevent
        >
          <!-- 基本信息 -->
          <div class="form-section">
            <div class="section-title">📋 基本信息</div>
            <el-row :gutter="24">
              <el-col :span="12">
                <el-form-item label="图书编码" prop="bookNo">
                  <el-input
                      v-model="bookForm.bookNo"
                      placeholder="请输入图书编码"
                      prefix-icon="Document"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="ISBN" prop="isbn">
                  <el-input
                      v-model="bookForm.isbn"
                      placeholder="请输入国际标准书号"
                      prefix-icon="Postcard"
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <el-row :gutter="24">
              <el-col :span="12">
                <el-form-item label="图书名称" prop="bookName">
                  <el-input
                      v-model="bookForm.bookName"
                      placeholder="请输入图书名称"
                      prefix-icon="Reading"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="图书类型" prop="bookType">
                  <el-select
                      v-model="bookForm.bookType"
                      placeholder="请选择图书类型"
                      style="width: 100%"
                  >
                    <el-option label="文学小说" value="文学小说" />
                    <el-option label="科学技术" value="科学技术" />
                    <el-option label="历史传记" value="历史传记" />
                    <el-option label="艺术设计" value="艺术设计" />
                    <el-option label="经济管理" value="经济管理" />
                    <el-option label="教育教学" value="教育教学" />
                    <el-option label="医学健康" value="医学健康" />
                    <el-option label="法律政治" value="法律政治" />
                    <el-option label="计算机" value="计算机" />
                    <el-option label="其他" value="其他" />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row :gutter="24">
              <el-col :span="12">
                <el-form-item label="作者" prop="author">
                  <el-input
                      v-model="bookForm.author"
                      placeholder="请输入作者姓名"
                      prefix-icon="User"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="出版社" prop="publisher">
                  <el-input
                      v-model="bookForm.publisher"
                      placeholder="请输入出版社名称"
                      prefix-icon="OfficeBuilding"
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <el-row :gutter="24">
              <el-col :span="12">
                <el-form-item label="出版日期" prop="publishDate">
                  <el-date-picker
                      v-model="bookForm.publishDate"
                      type="date"
                      placeholder="请选择出版日期"
                      style="width: 100%"
                      format="YYYY-MM-DD"
                      value-format="YYYY-MM-DD"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="库存数量" prop="quantity">
                  <el-input-number
                      v-model="bookForm.quantity"
                      :min="0"
                      :max="9999"
                      placeholder="请输入库存数量"
                      style="width: 100%"
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <el-form-item label="图书状态" prop="status">
              <el-radio-group v-model="bookForm.status">
                <el-radio :label="0">
                  <span class="status-tag">
                    <span class="status-dot status-available"></span>
                    可借阅
                  </span>
                </el-radio>
                <el-radio :label="1">
                  <span class="status-tag">
                    <span class="status-dot status-borrowed"></span>
                    借阅中
                  </span>
                </el-radio>
                <el-radio :label="2">
                  <span class="status-tag">
                    <span class="status-dot status-maintenance"></span>
                    维修中
                  </span>
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </div>

          <!-- 详细信息 -->
          <div class="form-section">
            <div class="section-title">📝 详细信息</div>
            <el-form-item label="图书简介" prop="description">
              <el-input
                  v-model="bookForm.description"
                  type="textarea"
                  :rows="4"
                  placeholder="请输入图书简介..."
                  maxlength="500"
                  show-word-limit
              />
            </el-form-item>
          </div>
        </el-form>

        <!-- 操作按钮 -->
        <div class="form-actions">
          <el-button @click="resetForm">重置</el-button>
          <el-button @click="goBack">返回</el-button>
          <el-button type="primary" @click="submitForm" :loading="loading">
            {{ loading ? '保存中...' : '保存' }}
          </el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, reactive, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import {addBook} from "@/api/book";

export default {
  name: 'AddBook',
  setup() {
    const mounted = ref(false);
    const bookFormRef = ref();
    const loading = ref(false);

    // 表单数据
    const bookForm = reactive({
      bookNo: '',
      bookName: '',
      bookType: '',
      author: '',
      description: '',
      publisher: '',
      publishDate: '',
      status: 0,
      isbn: '',
      quantity: 1
    });

    // 表单验证规则
    const formRules = {
      bookNo: [
        { required: true, message: '请输入图书编码', trigger: 'blur' },
        { min: 3, max: 20, message: '图书编码长度在 3 到 20 个字符', trigger: 'blur' }
      ],
      bookName: [
        { required: true, message: '请输入图书名称', trigger: 'blur' },
        { min: 1, max: 100, message: '图书名称长度不能超过 100 个字符', trigger: 'blur' }
      ],
      bookType: [
        { required: true, message: '请选择图书类型', trigger: 'change' }
      ],
      author: [
        { required: true, message: '请输入作者姓名', trigger: 'blur' },
        { max: 50, message: '作者姓名长度不能超过 50 个字符', trigger: 'blur' }
      ],
      publisher: [
        { required: true, message: '请输入出版社名称', trigger: 'blur' },
        { max: 100, message: '出版社名称长度不能超过 100 个字符', trigger: 'blur' }
      ],
      publishDate: [
        { required: true, message: '请选择出版日期', trigger: 'change' }
      ],
      isbn: [
        { required: true, message: '请输入ISBN', trigger: 'blur' },
        { pattern: /^[\d-X]+$/, message: 'ISBN格式不正确', trigger: 'blur' }
      ],
      quantity: [
        { required: true, message: '请输入库存数量', trigger: 'blur' },
        { type: 'number', min: 0, message: '库存数量不能小于0', trigger: 'blur' }
      ],
      status: [
        { required: true, message: '请选择图书状态', trigger: 'change' }
      ]
    };

    // 提交表单
    const submitForm = async () => {
      try {
        await bookFormRef.value?.validate();
        loading.value = true;
        console.log(bookForm)
        await addBook(bookForm)
        // 模拟API调用
        await new Promise(resolve => setTimeout(resolve, 1500));


        ElMessage.success('图书添加成功！');
        // 询问是否继续添加
        ElMessageBox.confirm('图书添加成功！是否继续添加新图书？', '操作成功', {
          confirmButtonText: '继续添加',
          cancelButtonText: '返回列表',
          type: 'success'
        }).then(() => {
          resetForm();
        }).catch(() => {
          goBack();
        });

      } catch (error) {
        ElMessage.error('请检查表单信息');
      } finally {
        loading.value = false;
      }
    };

    // 重置表单
    const resetForm = () => {
      bookFormRef.value?.resetFields();
      Object.assign(bookForm, {
        bookNo: '',
        bookName: '',
        bookType: '',
        author: '',
        description: '',
        publisher: '',
        publishDate: '',
        status: 0,
        isbn: '',
        quantity: 1
      });
      ElMessage.info('表单已重置');
    };

    // 返回
    const goBack = () => {
      window.history.back();
    };

    onMounted(() => {
      mounted.value = true;
    });

    return {
      mounted,
      bookFormRef,
      bookForm,
      formRules,
      loading,
      submitForm,
      resetForm,
      goBack
    };
  }
};
</script>

<style scoped>
.add-book-container {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;

  min-height: 100vh;
}

.header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 16px 24px;
  color: white;
  display: flex;
  align-items: center;
  box-shadow: 0 2px 12px rgba(0,0,0,0.1);
}

.header h1 {
  margin: 0;
  font-size: 24px;
  font-weight: 600;
}

.header .icon {
  margin-right: 12px;
  font-size: 28px;
}

.container {
  max-width: 1100px;
  margin: 24px auto;
  padding: 0 16px;
}

.form-card {
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.1);
  overflow: hidden;
}

.form-header {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
  padding: 20px 24px;
  color: white;
}

.form-header h2 {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
}

.form-content {
  padding: 32px;
}

.form-section {
  margin-bottom: 32px;
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin-bottom: 16px;
  padding-bottom: 8px;
  border-bottom: 2px solid #f0f0f0;
}

.form-actions {
  background: #f8f9fa;
  padding: 24px 32px;
  border-top: 1px solid #e9ecef;
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

.breadcrumb {
  margin-bottom: 16px;
}

.status-tag {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
}

.status-available { background-color: #52c41a; }
.status-borrowed { background-color: #faad14; }
.status-maintenance { background-color: #f5222d; }
</style>